MODULE boots
  USE GLOBVAR
  USE RANDOM
  USE STATISTICS
  USE MATRIX

  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE boot(momentboot)
    IMPLICIT NONE
    REAL(8), INTENT(INOUT):: momentboot(:,:) !initial guess
    REAL(8) :: a(N_ind,n_testt),momb(n_boot,n_mom),momb2(n_boot,n_mom2),wei(n_mom2,n_mom2)
    INTEGER :: i,j,k,kk,t,tt,m,mmm,n,l,indext(n_testt,2), artl(n_ind,n_levell,20),artc(n_ind,N_levelc,11),artf(n_ind,N_levelf,6),artg(n_ind,N_levelg,3)
    REAL(8),ALLOCATABLE:: AVSE(:),cor(:,:)
    REAL(8):: inter_l(n_ind,n_levell,20,3),interl_l(n_ind,n_levell,3),inter_c(n_ind,n_levelc,20,3),interc_c(n_ind,n_levelc,3),inter_f(n_ind,n_levelf,6,3),interf_f(n_ind,n_levelf,3)
    INTEGER:: du_l(n_levell),ind_l(N_ind,n_levell),du_c(n_levelc),ind_c(N_ind,n_levelc),du_f(n_levelf),ind_f(N_ind,n_levelf),&
        vindex(n_vnum+50),mm(n_ind),ii,jj
    
    !!!!! start to calculate data moments!!!!!
    
 IF (myid==0) THEN 
    
   DO ii=1,n_boot     
       jj=ii
        CALL SET_SEED(1438914432,234621+jj,1377777,123727)
                j=0
                k=0
                t=0
              DO i=1,N_vnum+50
                       vindex(i)=INT(SAMPLE_UNIFORM(1.0D0, DBLE(N_vnum+1)))
                       DO l=1,n_ind
                           IF (vid(l)==vindex(i)) t=l
                       END DO
                       j=k+vsum(t)
                       DO kk=k+1,j
                         IF (kk<=n_ind) mm(kk)=id_min(t)+j-kk
                          If(kk>n_ind) GO TO 36
                       END DO
                      k=j
             END DO
        !write(*,*) 
        !stop
36     DO i=1,N_ind

           mmboot(ii,i)=mm(i)
       END DO     
    
    !!! language tasks!!!!
    indext=0
    DO i=1,N_ind
      DO j=1,N_testt
         IF (t_test(mm(i),j)>-90 .and. levelg(j)<-90) THEN
             indext(j,1)=indext(j,1)+1
             IF (t_test(mm(i),j)>0.5) indext(j,2)=indext(j,2)+1
         END IF
         
      END DO
    END DO
    
    DO i=1,N_testt
        IF (indext(i,1)>0.5) THEN
            momb(ii,i)=DBLE(indext(i,2))/DBLE(indext(i,1)) ! PASSING RATE FOR EACH TASK
        ELSE 
            momb(ii,i)=-99
        END IF
    END DO
    
    !!!! generate artl variables !!!!
    artl=-1
    DO i=1,N_ind
        DO j=1,N_testt
            IF (t_test(mm(i),j)>-90 .and. levell(j)>-90) THEN
                k=levell(j)
                m=repl(mm(i),j)
                artl(i,k,m)=t_test(mm(i),j)
                inter_l(i,k,m,1:3)=x_testt(mm(i),j,1:3)
            END IF
        END DO
    END DO
    
    artc=-1
    DO i=1,N_ind
        DO j=1,N_testt
            IF (t_test(mm(i),j)>-90 .and. levelc(j)>-90) THEN
                k=levelc(j)
                m=repc(mm(i),j)
                artc(i,k,m)=t_test(mm(i),j)
                inter_c(i,k,m,1:3)=x_testt(mm(i),j,1:3)
            END IF
        END DO
    END DO
    
    artf=-1
    DO i=1,N_ind
        DO j=1,N_testt
            IF (t_test(mm(i),j)>-90 .and. levelf(j)>-90) THEN
                k=levelf(j)
                m=repf(mm(i),j)
                artf(i,k,m)=t_test(mm(i),j)
                inter_f(i,k,m,1:3)=x_testt(mm(i),j,1:3)
            END IF
        END DO
    END DO
    
    !artg=-1
    !DO i=1,N_ind
    !    DO j=1,N_testt
    !        IF (t_test(i,j)>-90 .and. levelg(j)>-90) THEN
    !            k=levelg(j)
    !            m=repg(i,j)
    !            artg(i,k,m)=t_test(i,j)
    !        END IF
    !    END DO
    !END DO
    
    !!!! passing rate for language tasks at each level!!!!

    DO j=N_testt+1,N_testt+N_levell
       m=0
       n=0 
       DO i=1,N_ind
           DO k=1,20
               IF (artl(i,j-N_testt,k)>-0.5) THEN
                   m=m+1
                   IF (artl(i,j-N_testt,k)==1) n=n+1
               END IF
           END DO 
       END DO
       IF (m>0) THEN
           momb(ii,j)=DBLE(n)/DBLE(m)
       ELSE
           momb(ii,j)=-99
       END IF
       
    END DO
    
    !!!! correlation across levels !!!!
    
    DO j=2,N_levell-1
      DO t=1,5
        DO k=1,5
          m=0
          n=0 
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j+1,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,N_testt+N_levell+(j-2)*25+(t-1)*5+k)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,N_testt+N_levell+(j-2)*25+(t-1)*5+k)=-99
          END IF
        END DO
      END DO
    END DO
    
    DO j=2,N_levell
        l=0
      DO t=1,4
        DO k=t+1,5
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,25*(N_levell-2)+N_testt+N_levell+(j-2)*10+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,25*(N_levell-2)+N_testt+N_levell+(j-2)*10+l)=-99
          END IF
        END DO
      END DO
    END DO
    
    DO j=2,N_levell-2
      DO t=1,5
        DO k=1,5
          m=0
          n=0 
          DO i=1,N_ind 
             IF (artl(i,j,t)==1 .and. artl(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artl(i,j,t)==1 .and. artl(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,25*(N_levell-2)+N_testt+N_levell+(N_levell-1)*10+(j-2)*25+(t-1)*5+k)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,25*(N_levell-2)+N_testt+N_levell+(N_levell-1)*10+(j-2)*25+(t-1)*5+k)=-99
          END IF
        END DO
      END DO
    END DO
    
    ! task passing rate for new enrolled group
    
    indext=0
    DO i=1,N_ind
      DO j=1,N_testt
          a(i,j)=firstt(mm(i),2)-month(j)-DBLE(time(j))/4.0d0
         IF (t_test(mm(i),j)>-90 .and. levelg(j)<-90 .and. a(i,j)>-1.25d0) THEN
             indext(j,1)=indext(j,1)+1
             IF (t_test(mm(i),j)>0.5) indext(j,2)=indext(j,2)+1
         END IF
         
      END DO
    END DO
    
    DO i=1,N_testt
        IF (indext(i,1)>0.5) THEN
            momb(ii,25*(2*N_levell-5)+N_testt+N_levell+(N_levell-1)*10+i)=DBLE(indext(i,2))/DBLE(indext(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,25*(2*N_levell-5)+N_testt+N_levell+(N_levell-1)*10+i)=-99
        END IF
    END DO
    
    ! task passing rate for the group enrollment longer than one month
    
    indext=0
    DO i=1,N_ind
      DO j=1,N_testt
          a(i,j)=DBLE(firstt(mm(i),2)-month(j)-DBLE(time(j))/4.0d0)

          
         IF (t_test(mm(i),j)>-90 .and. levelg(j)<-90 .and. a(i,j)<-1.25d0) THEN
             indext(j,1)=indext(j,1)+1
             IF (t_test(i,j)>0.5) indext(j,2)=indext(j,2)+1
         END IF
         
      END DO
    END DO


    
    DO i=1,N_testt
        IF (indext(i,1)>0.5) THEN
            momb(ii,25*(2*N_levell-5)+2*N_testt+N_levell+(N_levell-1)*10+i)=DBLE(indext(i,2))/DBLE(indext(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,25*(2*N_levell-5)+2*N_testt+N_levell+(N_levell-1)*10+i)=-99
        END IF
    END DO
    
       
        !!!! passing rate for cognitive tasks at each level!!!!
    
    kk=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10

    DO j=kk+1,kk+N_levelc
       m=0
       n=0 
       DO i=1,N_ind
           DO k=1,11
               IF (artc(i,j-kk,k)>-0.5) THEN
                   m=m+1
                   IF (artc(i,j-kk,k)==1) n=n+1
               END IF
           END DO 
       END DO
       IF (m>0) THEN
           momb(ii,j)=DBLE(n)/DBLE(m)
       ELSE
           momb(ii,j)=-99
       END IF
       
    END DO
    !!!! passing rate for fine motor tasks at each level!!!!
    
    kk=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc

    DO j=kk+1,kk+N_levelf
       m=0
       n=0 
       DO i=1,N_ind
           DO k=1,6
               IF (artf(i,j-kk,k)>-0.5) THEN
                   m=m+1
                   IF (artf(i,j-kk,k)==1) n=n+1
               END IF
           END DO 
       END DO
       IF (m>0) THEN
           momb(ii,j)=DBLE(n)/DBLE(m)
       ELSE
           momb(ii,j)=-99
       END IF
       
    END DO
    
    !    !!!! passing rate for gross motor tasks at each level!!!!
    !
    !kk=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf
    !
    !DO j=kk+1,kk+N_levelg
    !   m=0
    !   n=0 
    !   DO i=1,N_ind
    !       DO k=1,3
    !           IF (artg(i,j-kk,k)>-0.5) THEN
    !               m=m+1
    !               IF (artg(i,j-kk,k)==1) n=n+1
    !           END IF
    !       END DO 
    !   END DO
    !   IF (m>0) THEN
    !       momin(j)=DBLE(n)/DBLE(m)
    !   ELSE
    !       momin(j)=-99
    !   END IF
    !   
    !END DO
    
    
    !!!!! correlation across levels
    ! cognitive skills
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf
    
    l=0
    DO j=1,N_levelc-2
        IF (j<=2 .or. (j>4 .and. j<=8)) THEN
            tt=5
        ELSE IF (j>2 .and. j<5) THEN
            tt=2
        ELSE IF (j==9) THEN
            tt=2
        ELSE IF (j==10) THEN
            tt=4
        ELSE IF (j==11) THEN
            tt=5
        END IF
        
        IF (j<2 .or. (j>3 .and. j<8)) THEN
            kk=5
        ELSE IF (j>1 .and. j<4) THEN
            kk=2
        ELSE IF (j==8) THEN
            kk=2
        ELSE IF (j==9) THEN
            kk=4
        ELSE IF (j==10) THEN
            kk=5
        ELSE IF (j==11) THEN
            kk=3
        END IF
        
         DO t=1,tt
           DO k=1,kk
             m=0
             n=0 
             l=l+1
             DO i=1,N_ind 
                IF (artc(i,j,t)==1 .and. artc(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                    m=m+1
                    IF (artc(i,j,t)==1 .and. artc(i,j+1,k)==1) n=n+1
                END IF
             END DO
             
             IF (m>0) THEN
                momb(ii,mmm+l)=DBLE(n)/DBLE(m)
             ELSE
                momb(ii,mmm+l)=-99
             END IF
           END DO
         END DO
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177
    
   l=0 
   DO j=1,N_levelf-1
       IF (j==1) THEN
           tt=5
           kk=2
       ELSE IF (j==2) THEN
           tt=2
           kk=5
       ELSE IF (j==3) THEN
           tt=5
           kk=5
       ELSE IF (j==4) THEN
           tt=5
           kk=3
       ELSE IF(j==5) THEN
           tt=3
           kk=4
       ELSE IF (j==6) THEN
           tt=4
           kk=3
       END IF
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+1,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
   END DO
   
   mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84
   !
   !! gross motor skill
   !
   !l=0 
   !DO j=4,N_levelg-1
   !    IF (j==4) THEN
   !        tt=3
   !        kk=2
   !    ELSE IF (j==5) THEN
   !        tt=2
   !        kk=2
   !    ELSE IF (j==6) THEN
   !        tt=2
   !        kk=3
   !    ELSE IF (j==7) THEN
   !        tt=3
   !        kk=3
   !    ELSE IF(j==8) THEN
   !        tt=3
   !        kk=2
   !    END IF
   !   DO t=1,tt
   !     DO k=1,kk
   !       m=0
   !       n=0
   !       l=l+1
   !       DO i=1,N_ind 
   !          IF (artg(i,j,t)==1 .and. artg(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
   !              m=m+1
   !              IF (artg(i,j,t)==1 .and. artg(i,j+1,k)==1) n=n+1
   !          END IF
   !       END DO
   !       
   !       IF (m>0) THEN
   !          momin(mm+l)=DBLE(n)/DBLE(m)
   !       ELSE
   !          momin(mm+l)=-99
   !       END IF
   !     END DO
   !   END DO
   !END DO
   !
   !mm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+n_levelg+177+84+31
   
    ! cognitive
    
    l=0
    DO j=1,N_levelc-1
        IF (j<=2 .or. (j>4 .and. j<=8)) THEN
            tt=4
            kk=5
        ELSE IF (j>2 .and. j<5) THEN
            tt=1
            kk=2
        ELSE IF (j==9) THEN
            tt=1
            kk=2
        ELSE IF (j==10) THEN
            tt=3
            kk=4
        ELSE IF (j==11) THEN
            tt=4
            kk=5
        ELSE IF (j==12) THEN
            tt=2
            kk=3
        END IF
        
         DO t=1,tt
           DO k=t+1,kk
             m=0
             n=0 
             l=l+1
             DO i=1,N_ind 
                IF (artc(i,j,t)==1 .and. artc(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                    m=m+1
                    IF (artc(i,j,t)==1 .and. artc(i,j,k)==1) n=n+1
                END IF
             END DO
             
             IF (m>0) THEN
                momb(ii,mmm+l)=DBLE(n)/DBLE(m)
             ELSE
                momb(ii,mmm+l)=-99
             END IF
           END DO
         END DO
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82
    
    ! fine motor
    
    l=0
    DO j=1,N_levelf
        
       IF (j==1) THEN
           tt=4
           kk=5
       ELSE IF (j==2) THEN
           tt=1
           kk=2
       ELSE IF (j==3) THEN
           tt=4
           kk=5
       ELSE IF (j==4) THEN
           tt=4
           kk=5
       ELSE IF(j==5) THEN
           tt=2
           kk=3
       ELSE IF (j==6) THEN
           tt=3
           kk=4
       ELSE IF (j==7) THEN
           tt=2
           kk=3
       END IF
        
      DO t=1,tt
        DO k=t+1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43
    
    !! gross motor
    !
    !l=0
    !DO j=4,N_levelg
    !    
    !   IF (j==4) THEN
    !       tt=2
    !       kk=3
    !   ELSE IF (j==5) THEN
    !       tt=1
    !       kk=2
    !   ELSE IF (j==6) THEN
    !       tt=1
    !       kk=2
    !   ELSE IF (j==7) THEN
    !       tt=2
    !       kk=3
    !   ELSE IF(j==8) THEN
    !       tt=2
    !       kk=3
    !   ELSE IF (j==9) THEN
    !       tt=1
    !       kk=2
    !   END IF
    !    
    !  DO t=1,tt
    !    DO k=t+1,kk
    !      m=0
    !      n=0
    !      l=l+1
    !      DO i=1,N_ind 
    !         IF (artg(i,j,t)==1 .and. artg(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
    !             m=m+1
    !             IF (artg(i,j,t)==1 .and. artg(i,j,k)==1) n=n+1
    !         END IF
    !      END DO
    !      
    !      IF (m>0) THEN
    !         momin(mmm+l)=DBLE(n)/DBLE(m)
    !      ELSE
    !         momin(mmm+l)=-99
    !      END IF
    !    END DO
    !  END DO
    !END DO
    !
    !mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+84+43
    
    ! cognitive
    
    l=0
    DO j=1,N_levelc-3
        IF (j==1) THEN
            tt=5
            kk=2
        ELSE IF (j==2) THEN
            tt=5
            kk=2
        ELSE IF (j==3) THEN
            tt=2
            kk=5
        ELSE IF (j==4) THEN
            tt=2
            kk=5
        ELSE IF (j==5) THEN
            tt=5
            kk=5
        ELSE IF (j==6) THEN
            tt=5
            kk=5
        ELSE IF (j==7) THEN
            tt=5
            kk=2
        ELSE IF (j==8) THEN
            tt=5
            kk=4
        ELSE IF (j==9) THEN
            tt=2
            kk=5
        ELSE IF (j==10) THEN
            tt=4
            kk=3
        END IF
        
        
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0 
          l=l+1
          DO i=1,N_ind 
             IF (artc(i,j,t)==1 .and. artc(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artc(i,j,t)==1 .and. artc(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
    END DO  
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142
    
     ! fine motor
    
    l=0
    DO j=1,N_levelf-2
        IF (j==1) THEN
            tt=5
            kk=5
        ELSE IF (j==2) THEN
            tt=2
            kk=5
        ELSE IF (j==3) THEN
            tt=5
            kk=3
        ELSE IF (j==4) THEN
            tt=5
            kk=4
        ELSE IF (j==5) THEN
            tt=3
            kk=3
        END IF
        
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0 
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
    END DO  
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79
    
    !! gross motor
    !
    !l=0
    !DO j=4,N_levelg-2
    !    IF (j==4) THEN
    !        tt=3
    !        kk=2
    !    ELSE IF (j==5) THEN
    !        tt=2
    !        kk=3
    !    ELSE IF (j==6) THEN
    !        tt=2
    !        kk=3
    !    ELSE IF (j==7) THEN
    !        tt=3
    !        kk=2
    !    END IF
    !    
    !  DO t=1,tt
    !    DO k=1,kk
    !      m=0
    !      n=0 
    !      l=l+1
    !      DO i=1,N_ind 
    !         IF (artg(i,j,t)==1 .and. artg(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
    !             m=m+1
    !             IF (artg(i,j,t)==1 .and. artg(i,j+2,k)==1) n=n+1
    !         END IF
    !      END DO
    !      
    !      IF (m>0) THEN
    !         momin(mm+l)=DBLE(n)/DBLE(m)
    !      ELSE
    !         momin(mm+l)=-99
    !      END IF
    !    END DO
    !  END DO
    !END DO  
    !
    ! !mm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+n_levelg+177+84+31+84+43+12+142+79+24
       
    DUL_l=-99
    ind_l=-99
    DO i=1,N_ind
        DO k=2,N_levell
            DO j=1,20
                IF (artl(i,k,j)==1 .and. j==1) THEN
                    dul_l(i,k)=1
                    ind_l(i,k)=1
                ELSE IF (artl(i,k,j)==0 .and. j==1) THEN
                    dul_l(i,k)=j
                    ind_l(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==0 .and. ind_l(i,k)==0) THEN
                       dul_l(i,k)=j
                       ind_l(i,k)=0
                       IF (j<20 .and. artl(i,k,j+1)==-1) THEN
                           dul_l(i,k)=j+1
                           ind_l(i,k)=1
                       END IF                      
                           
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==0 .and. ind_l(i,k)==1) THEN
                        dul_l(i,k)=dul_l(i,k)
                        ind_l(i,k)=1
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==1 .and. ind_l(i,k)==0) THEN
                        dul_l(i,k)=j
                        ind_l(i,k)=1
                    ELSE IF (artl(i,k,j-1)==0 .and. artl(i,k,j)==1 .and. ind_l(i,k)==1) THEN
                        dul_l(i,k)=dul_l(i,k)
                        ind_l(i,k)=1
                    END IF
                END IF
            END DO           
                
        END DO
    END DO
    
    DO k=1,n_levell
        m=0
        n=0
        DO i=1,n_ind
            IF (dul_l(i,k)>-90) THEN
               m=m+1
               n=n+dul_l(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momb(ii,mmm+k)=DBLE(n)/DBLE(m)
          !con(mmm+k)=1 !m
        ELSE 
           momb(ii,mmm+k)=-99
           !con(mmm+k)=-99
        END IF
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+n_levell
    
    du_l=0
    DO k=1,n_levell
        DO i=1,N_ind
            IF (dul_l(i,k)>-90) THEN
                du_l(k)=du_l(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levell
        allocate(avse(du_l(k)))
        m=0
        DO i=1,n_ind
            IF (dul_l(i,k)>-90) THEN
                m=m+1
                avse(m)=dul_l(i,k)
            END IF
        END DO
        momb(ii,mmm+k)=STDEV_V(AVSE)
        !CON(MMM+K)=1
        DEALLOCATE(avse)
    END DO
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+2*n_levell
    

    IND_l=0
    interl_l=0.0d0
    DO k=2,N_levell
        DO i=1,N_ind
          DO j=1,20
              IF (inter_l(i,k,j,1)>-90) THEN
                  ind_l(i,k)=ind_l(i,k)+1
                  interl_l(i,k,1)=interl_l(i,k,1)+inter_l(i,k,j,1)
                  interl_l(i,k,2)=interl_l(i,k,2)+inter_l(i,k,j,2)
                  interl_l(i,k,3)=interl_l(i,k,3)+inter_l(i,k,j,3)
              END IF
          END DO
          IF (ind_l(i,k)>0) THEN
               interl_l(i,k,1)=DBLE(interl_l(i,k,1))/DBLE(ind_l(i,k))
               interl_l(i,k,2)=DBLE(interl_l(i,k,2))/DBLE(ind_l(i,k))
               interl_l(i,k,3)=DBLE(interl_l(i,k,3))/DBLE(ind_l(i,k))
          END IF
        END DO
    END DO
    
    
    du_l=0
    DO k=1,n_levell
        DO i=1,N_ind
            IF (ind_l(i,k)>0 .AND. dul_l(i,k)>-90) THEN
            du_l(k)=du_l(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levell
        allocate(cor(du_l(k),4))
        m=0
        DO i=1,n_ind
             IF (ind_l(i,k)>0 .AND. dul_l(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interl_l(i,k,1)
                cor(m,2)=interl_l(i,k,2)
                cor(m,3)=interl_l(i,k,3)
                cor(m,4)=dul_l(i,k)
             END IF
        END DO
        IF (k>1) THEN
        momb(ii,mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momb(ii,mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momb(ii,mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        !CON(MMM+3*(k-1)+1)=1 !du(k)
        !CON(MMM+3*(k-1)+2)=1 !du(k)
        !CON(MMM+3*(k-1)+3)=1 !du(k)
        ELSE 
            momb(ii,mmm+3*(k-1)+1)=-99
            momb(ii,mmm+3*(k-1)+2)=-99
            momb(ii,mmm+3*(k-1)+3)=-99
            !CON(MMM+3*(k-1)+1)=1
            !CON(MMM+3*(k-1)+2)=1
            !CON(MMM+3*(k-1)+3)=1
        END IF
        DEALLOCATE(cor)
    END DO
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell
    
    DUC_c=-99
    ind_c=-99
    DO i=1,N_ind
        DO k=1,N_levelc
            DO j=1,11
                IF (artc(i,k,j)==1 .and. j==1) THEN
                    duc_c(i,k)=1
                    ind_c(i,k)=1
                ELSE IF (artc(i,k,j)==0 .and. j==1) THEN
                    duc_c(i,k)=j
                    ind_c(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artc(i,k,j-1)==0 .and. artc(i,k,j)==0 .and. ind_c(i,k)==0) THEN
                       duc_c(i,k)=j
                       ind_c(i,k)=0
                       IF (j<11 ) THEN
                           IF (artc(i,k,j+1)==-1) THEN
                             duc_c(i,k)=j+1
                             ind_c(i,k)=1
                           END IF
                       END IF                      
                    ELSE IF (artc(i,k,j-1)==0 .and. artc(i,k,j)==0 .and. ind_c(i,k)==1) THEN
                        duc_c(i,k)=duc_c(i,k)
                        ind_c(i,k)=1
                    ELSE IF (artc(i,k,j-1)==0 .and. artc(i,k,j)==1 .and. ind_c(i,k)==0) THEN
                        duc_c(i,k)=j
                        ind_c(i,k)=1
                    ELSE IF (artc(i,k,j-1)==0 .and. artc(i,k,j)==1 .and. ind_c(i,k)==1) THEN
                        duc_c(i,k)=duc_c(i,k)
                        ind_c(i,k)=1
                    END IF
                END IF
            END DO           
        END DO
    END DO
    
    DO k=1,n_levelc
        m=0
        n=0
        DO i=1,n_ind
            IF (duc_c(i,k)>-90) THEN
               m=m+1
               n=n+duc_c(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momb(ii,mmm+k)=DBLE(n)/DBLE(m)
          !con(mmm+k)=1 !m
        ELSE 
           momb(ii,mmm+k)=-99
           !con(mmm+k)=-99
        END IF
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+n_levelc
    
    du_c=0
    DO k=1,n_levelc
        DO i=1,N_ind
            IF (duc_c(i,k)>-90) THEN
                du_c(k)=du_c(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levelc
        allocate(avse(du_c(k)))
        m=0
        DO i=1,n_ind
            IF (duc_c(i,k)>-90) THEN
                m=m+1
                avse(m)=duc_c(i,k)
            END IF
        END DO
        momb(ii,mmm+k)=STDEV_V(AVSE)
        !CON(MMM+K)=1 !du(k)
        DEALLOCATE(avse)
    END DO
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+2*n_levelc
    

    IND_c=0
    interc_c=0.0d0
    DO k=1,N_levelc
        DO i=1,N_ind
          DO j=1,11
              IF (inter_c(i,k,j,1)>-90) THEN
                  ind_c(i,k)=ind_c(i,k)+1
                  interc_c(i,k,1)=interc_c(i,k,1)+inter_c(i,k,j,1)
                  interc_c(i,k,2)=interc_c(i,k,2)+inter_c(i,k,j,2)
                  interc_c(i,k,3)=interc_c(i,k,3)+inter_c(i,k,j,3)
              END IF
          END DO
          IF (ind_c(i,k)>0) THEN
               interc_c(i,k,1)=DBLE(interc_c(i,k,1))/DBLE(ind_c(i,k))
               interc_c(i,k,2)=DBLE(interc_c(i,k,2))/DBLE(ind_c(i,k))
               interc_c(i,k,3)=DBLE(interc_c(i,k,3))/DBLE(ind_c(i,k))
          END IF
        END DO
    END DO
    
    
    du_c=0
    DO k=1,n_levelc
        DO i=1,N_ind
            IF (ind_c(i,k)>0 .AND. duc_c(i,k)>-90) THEN
            du_c(k)=du_c(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levelc
        allocate(cor(du_c(k),4))
        m=0
        DO i=1,n_ind
             IF (ind_c(i,k)>0 .AND. duc_c(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interc_c(i,k,1)
                cor(m,2)=interc_c(i,k,2)
                cor(m,3)=interc_c(i,k,3)
                cor(m,4)=duc_c(i,k)
             END IF
        END DO
        IF (k<n_levelc) THEN
        momb(ii,mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momb(ii,mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momb(ii,mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        !CON(MMM+3*(k-1)+1)=1
        !CON(MMM+3*(k-1)+2)=1
        !CON(MMM+3*(k-1)+3)=1
        ELSE 
            momb(ii,mmm+3*(k-1)+1)=-99
            momb(ii,mmm+3*(k-1)+2)=-99
            momb(ii,mmm+3*(k-1)+3)=-99
            !CON(MMM+3*(k-1)+1)=-99
            !CON(MMM+3*(k-1)+2)=-99
            !CON(MMM+3*(k-1)+3)=-99
        END IF
        DEALLOCATE(cor)
    END DO
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+5*n_levelc
    
        
    DUf_f=-99
    ind_f=-99
    DO i=1,N_ind
        DO k=1,N_levelf
            DO j=1,6
                IF (artf(i,k,j)==1 .and. j==1) THEN
                    duf_f(i,k)=1
                    ind_f(i,k)=1
                ELSE IF (artf(i,k,j)==0 .and. j==1) THEN
                    duf_f(i,k)=j
                    ind_f(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind_f(i,k)==0) THEN
                       duf_f(i,k)=j
                       ind_f(i,k)=0
                       IF (j<6) THEN
                           IF (artf(i,k,j+1)==-1) THEN
                           duf_f(i,k)=j+1
                           ind_f(i,k)=1
                           END IF
                       END IF                      
                           
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind_f(i,k)==1) THEN
                        duf_f(i,k)=duf_f(i,k)
                        ind_f(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind_f(i,k)==0) THEN
                        duf_f(i,k)=j
                        ind_f(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind_f(i,k)==1) THEN
                        duf_f(i,k)=duf_f(i,k)
                        ind_f(i,k)=1
                    END IF
                END IF
            END DO           
                
        END DO
    END DO
    
    DO k=1,n_levelf
        m=0
        n=0
        DO i=1,n_ind
            IF (duf_f(i,k)>-90) THEN
               m=m+1
               n=n+duf_f(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momb(ii,mmm+k)=DBLE(n)/DBLE(m)
          !con(mmm+k)=1
        ELSE 
           momb(ii,mmm+k)=-99
           !con(mmm+k)=-99
        END IF
    END DO
    
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+5*n_levelc+n_levelf  
    
    du_f=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (duf_f(i,k)>-90) THEN
                du_f(k)=du_f(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levelf
        allocate(avse(du_f(k)))
        m=0
        DO i=1,n_ind
            IF (duf_f(i,k)>-90) THEN
                m=m+1
                avse(m)=duf_f(i,k)
            END IF
        END DO
        momb(ii,mmm+k)=STDEV_V(AVSE)
        !CON(MMM+K)=1
        DEALLOCATE(avse)
    END DO
    mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+5*n_levelc+2*n_levelf 
    

    IND_f=0
    interf_f=0.0d0
    DO k=1,N_levelf
        DO i=1,N_ind
          DO j=1,6
              IF (inter_f(i,k,j,1)>-90) THEN
                  ind_f(i,k)=ind_f(i,k)+1
                  interf_f(i,k,1)=interf_f(i,k,1)+inter_f(i,k,j,1)
                  interf_f(i,k,2)=interf_f(i,k,2)+inter_f(i,k,j,2)
                  interf_f(i,k,3)=interf_f(i,k,3)+inter_f(i,k,j,3)
              END IF
          END DO
          IF (ind_f(i,k)>0) THEN
               interf_f(i,k,1)=DBLE(interf_f(i,k,1))/DBLE(ind_f(i,k))
               interf_f(i,k,2)=DBLE(interf_f(i,k,2))/DBLE(ind_f(i,k))
               interf_f(i,k,3)=DBLE(interf_f(i,k,3))/DBLE(ind_f(i,k))
          END IF
        END DO
    END DO
    
    
    du_f=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (ind_f(i,k)>0 .AND. duf_f(i,k)>-90) THEN
            du_f(k)=du_f(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levelf
        allocate(cor(du_f(k),4))
        m=0
        DO i=1,n_ind
             IF (ind_f(i,k)>0 .AND. duf_f(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interf_f(i,k,1)
                cor(m,2)=interf_f(i,k,2)
                cor(m,3)=interf_f(i,k,3)
                cor(m,4)=duf_f(i,k)
             END IF
        END DO
        !IF (k>1) THEN
        momb(ii,mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momb(ii,mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momb(ii,mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        !CON(MMM+3*(k-1)+1)=1
        !CON(MMM+3*(k-1)+2)=1
        !CON(MMM+3*(k-1)+3)=1
        !ELSE 
        !    momin(mmm+3*(k-1)+1)=-99
        !    momin(mmm+3*(k-1)+2)=-99
        !    momin(mmm+3*(k-1)+3)=-99
        !    CON(MMM+3*(k-1)+1)=-99
        !    CON(MMM+3*(k-1)+2)=-99
        !    CON(MMM+3*(k-1)+3)=-99
        !END IF
        DEALLOCATE(cor)
    END DO

mmm=25*(2*N_levell-5)+3*N_testt+N_levell+(N_levell-1)*10+n_levelc+n_levelf+177+84+82+43+142+79+5*n_levell+5*n_levelc+5*n_levelf 

   END DO !ii
   
 momentboot=momb
 
           OPEN(12,file="mmboot.out")
             DO i=1,N_ind
              WRITE(12,'(2000I4)') (mmboot(j,i),j=1,n_boot)
             END DO
           CLOSE(12)
  END IF  
    
  END SUBROUTINE BOOT
  
END MODULE BOOTS